/**
 * @Author: Jackey 327156719@qq.com
 * @Date: 2025-06-26 16:00:56
 * @LastEditors: Jackey 327156719@qq.com
 * @LastEditTime: 2025-06-26 16:59:20
 * @FilePath: src/main/java/org/springblade/common/utils/ivrcall/mns/AlicomMessagePullerService.java
 * @Description: 这是默认设置,可以在设置》工具》File Description中进行配置
 */
package org.springblade.common.utils.ivrcall.mns;


import com.alicom.mns.tools.DefaultAlicomMessagePuller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class AlicomMessagePullerService {

    private final ClientConfigDto clientConfigDto;
    private final MyMessageListener messageListener;

    public AlicomMessagePullerService(ClientConfigDto clientConfigDto, MyMessageListener messageListener) {
        this.clientConfigDto = clientConfigDto;
        this.messageListener = messageListener;
    }

    @PostConstruct
    public void startPuller() {
        try {
            log.info("启动阿里云轻量消息队列拉取服务，QueueName: {}", clientConfigDto.getQueueName());
            DefaultAlicomMessagePuller puller = new DefaultAlicomMessagePuller();

            // 开始拉取消息，持续监听
            puller.startReceiveMsg(
                    clientConfigDto.getAccessKeyId(),
                    clientConfigDto.getAccessKeySecret(),
                    clientConfigDto.getMessageType(),
                    clientConfigDto.getQueueName(),
                    messageListener);

            log.info("阿里云轻量消息队列拉取服务启动成功");
        } catch (Exception e) {
            log.error("启动阿里云轻量消息队列拉取服务失败", e);
        }
    }
}
